/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.prototype.console;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mdcolin
 */
public final class Index {
    /**.
     * Constructor
     */
    private Index() {

    }

    /**.
     * @param args
     * @throws FileNotFoundException
     */
    public static void main(final String[] args) throws FileNotFoundException {
        // store the data to show on
        StringBuilder showOn = new StringBuilder();
        // local path of the application
        String appPath = "C:\\Users\\Cmd\\Desktop\\M1_MIAGE\\CAR\\"
                + "Projet\\trunk\\trunk\\recover_sf_code2";
        // url of SF
        String path = "http://sourceforge.net";

        showOn.append("----------------------------------------\n");
        showOn.append("         Recover SourceForge Code       \n");
        showOn.append("----------------------------------------\n");

        CriteriaFile configFile = new CriteriaFile(
                "web\\WEB-INF\\criteria.properties");

        try {
            Connection connect;

            showOn.append("\nURL construit à partir du ficher conf :\n")
                    .append(path).append("/directory/")
                    .append(configFile.urlCriteria())
                    .append("\n\n");

            connect = new Connection(path + "/directory/"
                    + configFile.urlCriteria());

            int numWorkers = connect.nbPage();
            numWorkers--;

            ExecutorService pooldynamique = Executors.newCachedThreadPool();

            JobThread[] workers = new JobThread[numWorkers];
            Future<ConcurrentHashMap<String, String>>[] futures
                    = new Future[numWorkers];

            for (int i = 0; i < numWorkers; i++) {
                workers[i] = new JobThread(connect.urlPage().get(i));
                futures[i] = pooldynamique.submit(workers[i]);
            }

            pooldynamique.shutdown();

            for (int i = 0; i < numWorkers; i++) {
                try {
                    connect.getConcurrentMap().putAll((futures[i].get()));
                } catch (Exception ex) {
                    Logger.getLogger(Index.class.getName())
                            .log(Level.SEVERE, null, ex);
                }
            }

            PoolThreadCmdLineRecover cmdline =
                        new PoolThreadCmdLineRecover(appPath,
                    "\\ProjectsRecover\\",
                    path + "/directory/"
                    + configFile.urlCriteria());

            cmdline.getProjects(path);
            // show svn, git, cvs path of the projects
            ArrayList<String> tab = cmdline.getTab();
            for (int i = 0; i < tab.size(); i++) {
                if (tab.get(i) != null) {
                    showOn.append("--").
                            append(tab.get(i).toString()).append("\n");
                } else {
                    showOn.append("--null").append("\n");
                }
            }

            System.out.println(showOn);
        } catch (InterruptedException ex) {
            Logger.getLogger(Index.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
